﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VoiceAd.Models;
using VoiceAd.DAL;

namespace VoiceAd.Controllers
{
    public class ProvinceController : Controller
    {
        //
        // GET: /Province/
        private IProvinceRepository provinceRepository = new ProvinceRepository(new dbVoiceAdDataContext());

        public ActionResult IndexProvince()
        {
            ViewBag.LstProvinces = provinceRepository.GetListProvinces();
            return View();
        }

        //
        // GET: /Province/Details/5

        public ActionResult DetailsProvince(int id)
        {
            tblProvince tbl = provinceRepository.GetProvinceById(id);
            return View(tbl);
        }

        //
        // GET: /Province/Create

        public ActionResult CreateProvince()
        {

            return View();
        } 

        //
        // POST: /Province/Create

        [HttpPost]
        public ActionResult CreateProvince(tblProvince tbl)
        {
            try
            {

                if (ModelState.IsValid)
                {
                    provinceRepository.InsProvince(tbl);
                    return RedirectToAction("Index");
                }               
            }
            catch
            {
                ModelState.AddModelError("", "Đã có lỗi xảy ra trong quá trình lưu");                
            }
            return View(tbl);
        }
        
        //
        // GET: /Province/Edit/5

        public ActionResult EditProvince(int id)
        {
            return View(provinceRepository.GetProvinceById(id));           
        }

        //
        // POST: /Province/Edit/5

        [HttpPost]
        public ActionResult EditProvince(tblProvince tbl)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    provinceRepository.UpdProvince(tbl);
                    return RedirectToAction("Index");
                }   
            }
            catch
            {
                return View();
            }
            return View(tbl);
        }

        //
        // GET: /Province/Delete/5

        public ActionResult DeleteProvince(int id)
        {
            tblProvince tbl = provinceRepository.GetProvinceById(id);
            return View(tbl);
        }

        //
        // POST: /Province/Delete/5

        [HttpPost]
        public ActionResult DeleteProvince(int id, FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    provinceRepository.DelProvince(id);
                    return RedirectToAction("Index");
                }
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
